package com.sunriz.motordrive.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class CheckException {
    public static Exception eventListenerException;

    // 注入全局异常处理器
    @Autowired
    private GlobalExceptionHandler globalHandler;

    // 定时检查异常的方法
    public void checkForExceptions() {
        if (eventListenerException != null) {
            if (eventListenerException instanceof CrcValidationException) {
                globalHandler.handleCrcValidationException((CrcValidationException) eventListenerException);
            } else if (eventListenerException instanceof NullPointerException){
                globalHandler.handleGlobalException((NullPointerException)eventListenerException);
            }
            // 重置异常变量
            eventListenerException = null;
        }
    }

    // 定时任务设置
    @Scheduled(fixedRate = 500)
    public void scheduleCheckForExceptions() {
        checkForExceptions();
    }
}
